iT邦幫忙

2025 iThome 鐵人賽

DAY 8
1
Cloud Native

《30 天 Cloud Native:AI 行程規劃後端開發》系列 第 8

Day8 - 部署 AI Agent 到 Vertex AI Agent Engine

  • 分享至 

  • xImage
  •  

昨天我們學會怎麼在 Cloud Run Service 上建立 AI Agent,但文件上說 Vertex AI 能夠由 Google Cloud 完美地管理,因此今天來試著將 AI Agent 部署到 Vertex AI Agent Engine 上。

參考文件與專案架構

今天參考的文件是 Agent Development Kit,但看不懂專案架構,因此又去參考 Quickstart: Develop and deploy agents on Vertex AI Agent Engine,才發現原來這些程式碼是連貫的,要放在同個 Python 檔案內,所以大致上的結構如下:

# ==== init ====
import vertexai
from vertexai import agent_engines

vertexai.init(...)
# ==== agent ====
from google.adk.agents import Agent
from vertexai.preview.reasoning_engines import AdkApp

agent = Agent(...)
app = AdkApp(agent=agent)
# ==== test ====
for event in app.stream_query(...):
    print(event)

這樣就可以在本地執行,而遠端的結構則是:

# ==== init ====
...
# ==== agent ====
...
app = AdkApp(agent=agent)
# ==== deploy ====
from vertexai import agent_engines

remote_agent = agent_engines.create(app, ...)
# ==== remote test ====
for event in remote_agent.stream_query(...):
    print(event)

建立 Bucket 並設定 staging_bucket

在執行之前要先到 Buckets 頁面建立一個新的,設定的名稱要填回 staging_bucket 參數裡(這邊的名稱蠻容易撞的,要想個獨一無二的名字):

https://ithelp.ithome.com.tw/upload/images/20250820/20178194VnE92BCJxg.png

注意事項與錯誤排查

要注意 agent_engines.create 只要執行一次就會建立一次,要小心不要一直執行它。但我為了讓程式碼整潔,把昨天的 multi_tool_agent 當作 Python package 包起來取代 agent 變數:

# ==== init ====
...
# ==== agent ====
from multi_tool_agent.agent import root_agent
agent = root_agent
# ==== deploy ====
...

在本地測試的時候沒問題,但是當要部屬到 Vertex AI Agent Engine 上時就出問題了,指令回傳的錯誤為:

google.api_core.exceptions.InvalidArgument: 400 Reasoning Engine resource [...] failed to start and cannot serve traffic. Please refer to our documentation (https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/troubleshooting/deploy) for checking logs and other troubleshooting tips. 3: Reasoning Engine resource [...] failed to start and cannot serve traffic. Please refer to our documentation (https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/troubleshooting/deploy) for checking logs and other troubleshooting tips.

這個時候可以到 Logs Explorer 找問題,就會看到有很多 Error message :

https://ithelp.ithome.com.tw/upload/images/20250820/201781948nCa3qOjMp.png

說他找不到我的 multi_tool_agent ,如果到剛剛建立的 Bucket 裡面會看到這些:

https://ithelp.ithome.com.tw/upload/images/20250820/20178194shyxsR1Rf3.png

看起來剛剛寫的程式碼會被序列化存到 Bucket 內,但因為我的 multi_tool_agent 是一個 Package,且沒有設定相依性就導致我的 Agent 沒被上傳上去。

後續計畫

雖然成功將 Agent 部署到 Vertex AI 上了,但是自己寫的 Agent 就沒有辦法 CD,明天再看看文件有沒有什麼方法能夠做到。


上一篇
Day7 - 在 Cloud Run 建立 AI Agent
下一篇
Day9 - 探索 Vertex AI Agent Engine 的部署流程與挑戰
系列文
《30 天 Cloud Native:AI 行程規劃後端開發》18
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言